# 客户端代码，模拟文件下载
import socket


# 发送数据到服务器
def send_2_server(client_socket, file_name):
    client_socket.send(file_name.encode("utf-8"))


# 读取服务器数据写入文件
def rev_data(client_socket, file_name):
    # 获取下载数据
    msg = client_socket.recv(1024)
    # 如果有文件才写入
    if msg and msg != "error":
        # 将数据写入文件
        with open(file_name, "wb") as f:
            f.write(msg)
    else:
        print("没有该文件!!!")


def run():
    # 创建客户端套接字
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    address = ("192.168.153.1", 8080)
    # 链接服务器
    client_socket.connect(address)
    # 发送下载文件名
    file_name = "1111.txt"
    # 发送数据
    send_2_server(client_socket, file_name)
    # 写入文件
    rev_data(client_socket, file_name)

    # 关闭服务
    client_socket.close()


if __name__ == "__main__":
    run()
